import time

from hytest import STEP, INFO, CHECK_POINT, GSTORE
from lib.func_name import *
from lib.button import *
from lib.share import *


class UI_1101:
    name = 'UI-A1101'

    def teardown(self):
        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_info_button)

    def teststeps(self):
        GSTORE['thomework_create'].submit()
        ret_text = GSTORE['thomework_create'].get_txt(GSTORE['thomework_create'].loc_info_text)
        CHECK_POINT('提示信息：请输入作业名称',
                    ret_text == '请输入作业名称')


class UI_1102:
    ddt_cases = [
        {
            'name': 'UI-1102',
            'para': 'homework_001'
        },
    ]

    def teardown(self):
        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_info_button)
        GSTORE['thomework_create'].clear(GSTORE['thomework_create'].loc_homework_name)

    def teststeps(self):
        homework_name = self.para
        STEP(1, '输入作业名称')
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        INFO(homework_name)

        STEP(2, '点击添加按钮')
        GSTORE['thomework_create'].submit()
        STEP(3, '获取并检查提示信息')
        ret_text = GSTORE['thomework_create'].get_txt(GSTORE['thomework_create'].loc_info_text)
        CHECK_POINT('提示信息：请添加题目到作业',
                    ret_text == '请添加题目到作业')


class UI_1103:
    txt1 = 'a' * 100 + 'b' * 20
    ddt_cases = [
        {'name': 'UI-1103',
         'para': txt1}
    ]

    def teardown(self):
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        homework_name = self.para
        obj1 = GSTORE['thomework_create']
        obj2 = GSTORE['iframe_homework_create']
        obj4 = GSTORE['thomework_created']

        flow1_dict = {
            'obj': obj1,
            func_create_homework_name_input: homework_name,
            func_create_select_question: None
        }

        flow2_dict = {
            'obj': obj2,
            func_iframe_jump: None,
            func_iframe_click_floor_tree: {
                'loc_method': obj2.loc_question_method,
                'loc_value_input': obj2.loc_question_add_value,
                'index1': 1
            },
            func_click_button: button_确定,
            func_iframe_leave: None
        }

        flow3_dict = {
            'obj': obj1,
            func_create_submit: None,
            'click': obj1.loc_not_release
        }

        flow4_dict = {
            'obj': obj4,
            func_get_txt_floor: {
                'loc_method': obj4.loc_question_method,
                'loc_value_input': obj4.created_homework_name,
                'index1': 1
            }
        }
        STEP(1, '输入作业名(超过100位)')
        obj1.workflow(flow1_dict)

        STEP(2, '选择习题')
        ret = obj2.workflow(flow2_dict)
        CHECK_POINT('选题操作正常执行', ret == None or ret['ret'] == 0)

        STEP(3, '暂不发布')
        ret = obj1.workflow(flow3_dict)

        STEP(4, '获取作业名')
        ret = obj4.workflow(flow4_dict)
        INFO(ret['msg'])

        CHECK_POINT('作业名称长度为100', len(ret['msg']) == 100)


class UI_A4101:
    ddt_cases = [
        {'name': 'UI-A4101', 'para': '占位用'},
        {'name': 'UI-A4102', 'para': '占位用'},
        {'name': 'UI-A4201', 'para': '占位用'},
        {'name': 'UI-A4202', 'para': '占位用'},
    ]

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name

    def teardown(self):
        GSTORE['thome'].return_home()
        time.sleep(0.5)
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        # obj1 = GSTORE['thome']
        obj2 = GSTORE['thomework_create']
        obj3 = GSTORE['iframe_homework_create']
        obj4 = GSTORE['thomework_created']
        # obj5 = GSTORE['question_info']
        obj6 = GSTORE['release_to_student']

        STEP(1, '创建作业')
        if self.name in ['UI-A4101', 'UI-A4201']:
            obj3.click_floor(obj3.loc_question_method,
                             obj3.loc_question_add_value,
                             index1=1)
        if self.name in ['UI-A4102', 'UI-A4202']:
            obj3.click_button(button_本页全加入)
        obj3.click_button(button_确定)
        GSTORE['iframe_homework_create'].leave_iframe()
        obj2.submit()

        if self.name in ['UI-A4101', 'UI-A4102']:
            STEP(2, '暂不发布')
            obj2.click(obj2.loc_pop_not_release)
            time.sleep(1)  # 不可删除强制等待

            STEP(3, '查看做成作业')
            homework_name = obj4.get_txt_floor(loc_method=obj4.loc_question_method,
                                               loc_value_input=obj4.created_homework_name,
                                               index1=1)['msg']
            INFO(homework_name)
            CHECK_POINT('查看创建作业是否被生成', homework_name == GSTORE['homework_name'])

        if self.name in ['UI-A4201', 'UI-A4202']:
            STEP(2, '发布作业')
            obj2.click(obj2.loc_pop_release)
            time.sleep(1)  # 不可删除强制等待

            main_window = obj2.jump_window('下发学习任务')
            page_title = obj6.get_txt(obj6.loc_title)
            INFO(page_title)
            CHECK_POINT('进入下发学习任务页面', page_title == '下发学习任务')
            obj6.close()
            obj6.return_main_window(main_window)


class UI_A5103:
    ddt_cases = [
        {'name': 'UI-A5103', 'para': 1},
        {'name': 'UI-A5104', 'para': 2},
        {'name': 'UI-A5105', 'para': 8},
        {'name': 'UI-A5106', 'para': 6},
        {'name': 'UI-A5107', 'para': 13},
        {'name': 'UI-A5203', 'para': 1},
        {'name': 'UI-A5204', 'para': 2},
        {'name': 'UI-A5205', 'para': 2},
        {'name': 'UI-A5206', 'para': 4},
        {'name': 'UI-A5207', 'para': 5},
        {'name': 'UI-A5301', 'para': 2},
        {'name': 'UI-A5302', 'para': 1},
    ]

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name
        GSTORE['iframe_homework_create'].click_buttons([button_本页全加入, button_确定])
        GSTORE['iframe_homework_create'].leave_iframe()
        GSTORE['thomework_create'].submit()

        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_pop_release)
        time.sleep(1)  # 不可删除强制等待
        GSTORE['main_window'] = GSTORE['thomework_create'].jump_window('下发学习任务')

    def teardown(self):
        GSTORE['thome'].return_home()
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        obj1 = GSTORE['thome']
        #         obj2 = GSTORE['thomework_create']
        #         obj3 = GSTORE['iframe_homework_create']
        #         obj4 = GSTORE['thomework_created']
        #         obj5 = GSTORE['question_info']
        obj6 = GSTORE['release_to_student']
        obj7 = GSTORE['release']

        check_count = self.para
        STEP(1, '选择发布学生并发布')
        if self.name in ['UI-A5203', 'UI-A5204', 'UI-A5205',
                         'UI-A5206', 'UI-A5207', 'UI-A5301', 'UI-A5302']:
            obj6.click_box(3)
            time.sleep(0.5)
            obj6.click_box(4)
            time.sleep(0.5)

        if self.name in ['UI-A5103']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class1,
                             index1=1)
        if self.name in ['UI-A5104']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class1,
                             index1=1)
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class2,
                             index1=1)
        if self.name in ['UI-A5105']:
            obj6.click(obj6.loc_all_choices, 1)
        if self.name in ['UI-A5106']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class1,
                             index1=1)
            obj6.click(obj6.loc_all_choices, 2)
        if self.name in ['UI-A5107']:
            obj6.click(obj6.loc_all_choices, 1)
            obj6.click(obj6.loc_all_choices, 2)
        if self.name in ['UI-A5203']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup1,
                             index1=1)
        if self.name in ['UI-A5204']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup1,
                             index1=1)
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup2,
                             index1=1)
        if self.name in ['UI-A5205']:
            obj6.click(obj6.loc_all_choices, 3)
        if self.name in ['UI-A5206']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup1,
                             index1=1)
            obj6.click(obj6.loc_all_choices, 4)
        if self.name in ['UI-A5207']:
            obj6.click(obj6.loc_all_choices, 3)
            obj6.click(obj6.loc_all_choices, 4)
        if self.name in ['UI-A5301']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class1,
                             index1=1)
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup2,
                             index1=1)
        if self.name in ['UI-A5302']:
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_class1,
                             index1=1)
            obj6.click_floor(obj6.loc_method_xpath,
                             obj6.loc_floor_value_gloup1,
                             index1=1)

        for i in [obj6.loc_submit_to_release,
                  obj6.loc_pop_submit,
                  obj6.loc_pop2_submit]:
            obj6.click(i)
            time.sleep(0.5)

        obj6.return_main_window(GSTORE['main_window'])

        obj1.open_homework_released()

        STEP(2, '获取发布学生人数')
        obj7.click_floor(xpath, obj7.loc_complate_info_value, index1=1)
        release_student_count = obj7.release_student_count()
        INFO(f'发布人数：{release_student_count}')
        CHECK_POINT(f'该作业向{check_count}人发布', release_student_count == check_count)


class UI_B2301:
    name = 'UI-B2301'

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name
        GSTORE['iframe_homework_create'].click_buttons([button_本页全加入, button_确定])
        GSTORE['iframe_homework_create'].leave_iframe()
        GSTORE['thomework_create'].submit()

        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_pop_not_release)
        time.sleep(1)  # 不可删除强制等待

    def teardown(self):
        GSTORE['thome'].return_home()
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        # obj1 = GSTORE['thome']
        # obj2 = GSTORE['thomework_create']
        # obj3 = GSTORE['iframe_homework_create']
        obj4 = GSTORE['thomework_created']
        # obj5 = GSTORE['question_info']
        # obj6 = GSTORE['release_to_student']
        # obj7 = GSTORE['release']

        homework_count1 = len(obj4.locates(obj4.loc_created_question))
        obj4.click_floor(xpath, obj4.button_created_delete, index1=1)
        time.sleep(0.5)
        obj4.click(obj4.loc_delete_pop_submit)
        time.sleep(0.5)
        homework_count2 = len(obj4.locates(obj4.loc_created_question))
        CHECK_POINT('目标作业被删除', homework_count1 - homework_count2 == 1)


class UI_B2401:
    name = 'UI-B2401'

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name
        GSTORE['iframe_homework_create'].click_buttons([button_本页全加入, button_确定])
        GSTORE['iframe_homework_create'].leave_iframe()
        GSTORE['thomework_create'].submit()

        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_pop_not_release)
        time.sleep(1)  # 不可删除强制等待

    def teardown(self):
        GSTORE['thome'].return_home()
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        # obj1 = GSTORE['thome']
        # obj2 = GSTORE['thomework_create']
        # obj3 = GSTORE['iframe_homework_create']
        obj4 = GSTORE['thomework_created']
        # obj5 = GSTORE['question_info']
        obj6 = GSTORE['release_to_student']
        # obj7 = GSTORE['release']

        obj4.click_floor(xpath, obj4.button_created_released, index1=1)
        main_window = obj4.jump_window('下发学习任务')
        page_title = obj6.get_txt(obj6.loc_title)
        obj6.driver.close()
        obj6.return_main_window(main_window)

        CHECK_POINT('进入下发学习任务界面', page_title == '下发学习任务')


class UI_B2402:
    name = 'UI-B2402'

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name
        GSTORE['iframe_homework_create'].click_buttons([button_本页全加入, button_确定])
        GSTORE['iframe_homework_create'].leave_iframe()
        GSTORE['thomework_create'].submit()

        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_pop_not_release)
        time.sleep(1)  # 不可删除强制等待

    def teardown(self):
        GSTORE['thome'].return_home()
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        # obj1 = GSTORE['thome']
        # obj2 = GSTORE['thomework_create']
        # obj3 = GSTORE['iframe_homework_create']
        obj4 = GSTORE['thomework_created']
        # obj5 = GSTORE['question_info']
        obj6 = GSTORE['release_to_student']
        obj7 = GSTORE['release']

        obj4.click_floor(xpath, obj4.button_created_released, index1=1)
        main_window = obj4.jump_window('下发学习任务')

        obj6.click_floor(xpath, obj6.loc_floor_value_class1, index1=1)
        obj6.click(obj6.loc_submit_to_release)
        obj6.click(obj6.loc_pop_submit)
        time.sleep(0.5)
        obj6.click(obj6.loc_pop2_submit)
        time.sleep(0.5)

        obj6.return_main_window(main_window)
        obj4.open_homework_released()

        obj7.click_floor(xpath, obj7.loc_complate_info_value, index1=1)
        released_count = obj7.release_student_count()
        CHECK_POINT('作业成功下发', released_count == 1)


class UI_C3001:
    name = 'UI-C3001'

    def setup(self):
        time_now = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        homework_name = f'homework_{time_now}'
        GSTORE['thomework_create'].input(GSTORE['thomework_create'].loc_homework_name, homework_name)
        GSTORE['thomework_create'].select_question()
        GSTORE['iframe_homework_create'].jump()
        GSTORE['homework_name'] = homework_name
        GSTORE['iframe_homework_create'].click_buttons([button_本页全加入, button_确定])
        GSTORE['iframe_homework_create'].leave_iframe()
        GSTORE['thomework_create'].submit()

        GSTORE['thomework_create'].click(GSTORE['thomework_create'].loc_pop_release)
        time.sleep(1)  # 不可删除强制等待
        GSTORE['main_window'] = GSTORE['thomework_create'].jump_window('下发学习任务')

        GSTORE['release_to_student'].click_floor(xpath,GSTORE['release_to_student'].loc_floor_value_class1,index1=1)
        GSTORE['release_to_student'].click(GSTORE['release_to_student'].loc_submit_to_release)
        GSTORE['release_to_student'].click(GSTORE['release_to_student'].loc_pop_submit)
        GSTORE['release_to_student'].click(GSTORE['release_to_student'].loc_pop2_submit)
        GSTORE['release_to_student'].return_main_window(GSTORE['main_window'])
        GSTORE['thome'].open_homework_released()

    def teardown(self):
        GSTORE['thome'].return_home()
        GSTORE['thome'].open_homework_create()

    def teststeps(self):
        # obj1 = GSTORE['thome']
        # obj2 = GSTORE['thomework_create']
        # obj3 = GSTORE['iframe_homework_create']
        # obj4 = GSTORE['thomework_created']
        # obj5 = GSTORE['question_info']
        # obj6 = GSTORE['release_to_student']
        obj7 = GSTORE['release']

        check_txt1_1 = obj7.get_txt_floor(xpath,obj7.loc_release_time_value,index1=1)
        check_txt1_2 = obj7.get_txt_floor(xpath,obj7.loc_release_time_value,index1=2)
        obj7.click_floor(xpath,obj7.loc_delete_value,index1=1)
        obj7.click(obj7.loc_delete_submit)

        check_txt2_1 = obj7.get_txt_floor(xpath, obj7.loc_release_time_value, index1=1)
        CHECK_POINT('作业被撤销',check_txt1_2==check_txt2_1)